package com.globant.twittersearch;

import com.globant.dao.TweetSQLHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class TweetContentProvider extends ContentProvider {

	private static final String uri = "content://com.globant.twittersearch/tweet";
	public static final Uri CONTENT_URI = Uri.parse(uri);
    
    private TweetSQLHelper tweetdbh;
    private static final String BD_NOMBRE = "DBTweets";
    private static final int BD_VERSION = 1;
    private static final String TABLA_TWEET = "tweet";
    
    private static final int TWEETS = 1;
    private static final int TWEETS_ID = 2;
    private static final UriMatcher uriMatcher;

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI("com.globant.twittersearch", "tweet", TWEETS);
        uriMatcher.addURI("com.globant.twittersearch", "tweet/#", TWEETS_ID);
    }
    
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int cont;
		String where = selection;
		if (uriMatcher.match(uri) == TWEETS_ID) {
			where = "_id=" + uri.getLastPathSegment();
		}

		SQLiteDatabase db = tweetdbh.getWritableDatabase();

		cont = db.delete(TABLA_TWEET, where, selectionArgs);

		return cont;
	}

	@Override
	public String getType(Uri uri) {
	    int match = uriMatcher.match(uri);
	    
		switch (match) {
			case TWEETS:
				return "vnd.android.cursor.dir/vnd.globant.tweet";
			case TWEETS_ID:
				return "vnd.android.cursor.item/vnd.globant.tweet";
			default:
				return null;
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
	    long regId = 1;
	    
	    SQLiteDatabase db = tweetdbh.getWritableDatabase();
	 
	    regId = db.insert(TABLA_TWEET, null, values);
	 
	    Uri newUri = ContentUris.withAppendedId(CONTENT_URI, regId);
	 
	    return newUri;
	}

	@Override
	public boolean onCreate() {
		tweetdbh = new TweetSQLHelper(getContext(), BD_NOMBRE, null, BD_VERSION);
		SQLiteDatabase database = tweetdbh.getWritableDatabase();
		return database != null && database.isOpen();
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
	    String where = selection;
		if (uriMatcher.match(uri) == TWEETS_ID) {
			where = "_id=" + uri.getLastPathSegment();
		}

		SQLiteDatabase db = tweetdbh.getWritableDatabase();

		Cursor c = db.query(TABLA_TWEET, projection, where, selectionArgs,
				null, null, sortOrder);

		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int cont;
		String where = selection;
		if (uriMatcher.match(uri) == TWEETS_ID) {
			where = "_id=" + uri.getLastPathSegment();
		}

		SQLiteDatabase db = tweetdbh.getWritableDatabase();

		cont = db.update(TABLA_TWEET, values, where, selectionArgs);

		return cont;
	}

}
